package com.liuning.graduationdemo.controller;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.liuning.graduationdemo.dto.AliPay;
import com.liuning.graduationdemo.service.IOrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/alipay")
public class AliPayController {

    @Resource
    private IOrderService orderService;

    /**
     * 调用支付接口
     * @param aliPay
     * @return
     */
    @GetMapping("/pay")
    public String pay(AliPay aliPay) {
        AlipayTradePagePayResponse response;
        try {
            //发起API调用，以创建收款二维码为例
            response = Factory.Payment.Page()
                    .pay(aliPay.getSubject(),aliPay.getTraceNo(),aliPay.getTotalAmount(),"");
        }catch (Exception e) {
            System.out.println("调用异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(),e);
        }
        return response.getBody();
    }

    /**
     * 接收成功失败结果
     * 必须是post接口
     * @param request
     * @return
     */
    @PostMapping("/notify")
    public String payNotify(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("======支付宝异步回调======");

            Map<String, String> params = new HashMap<>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name,request.getParameter(name));
                System.out.println(name + " = " + request.getParameter(name));
            }

            //商户订单号
            String tradeNo = params.get("out_trade_no");
            //买家付款时间
            String gmtPayment = params.get("gmt_payment");

            //支付宝验签
            if (Factory.Payment.Common().verifyNotify(params)) {
                //验签通过
                System.out.println("交易名称：" + params.get("subject"));
                System.out.println("交易状态：" + params.get("trade_status"));
                System.out.println("支付宝交易凭证号：" + params.get("trade_no"));
                System.out.println("商户订单号：" + params.get("out_trade_no"));
                System.out.println("交易金额：" + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id：" + params.get("buyer_id"));
                System.out.println("买家付款时间：" + params.get("gmt_payment"));
                System.out.println("买家付款金额：" + params.get("buyer_pay_amount"));

                //更新订单为已经支付
//                orderMapper.updateStatus(tradeNo,1,gmtPayment);
                int res = orderService.updateState(tradeNo,1,gmtPayment);
            }
        }
        return "success";
    }
}
